﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;
using DevExpress.XtraSplashScreen;

namespace Baubixinh_client
{
    public partial class frmImportNew : DevExpress.XtraEditors.XtraForm
    {
        private DataTable mobjKhoHang = null;
        private DataTable mobjMauSac = null;
        private DataTable mobjSize = null;
        private DataRow mobjRow = null;

        public frmImportNew()
        {
            InitializeComponent();
        }

        private void frmImportNew_Load(object sender, EventArgs e)
        {
            try
            {
                mobjKhoHang = clsDbAccess.fncGetAttributeValue(Program.gobjDb, clsEnum.ATTRIBUTE.STORE);
                mobjMauSac = clsDbAccess.fncGetAttributeValue(Program.gobjDb, clsEnum.ATTRIBUTE.COLOR);
                mobjSize = clsDbAccess.fncGetAttributeValue(Program.gobjDb, clsEnum.ATTRIBUTE.SIZE);

                //tao datasource cho  grid
                DataTable objDataGrid;
                DataTable objDataStaff;

                objDataGrid = new DataTable();
                objDataGrid.Columns.Add("product_id");
                objDataGrid.Columns.Add("product_name");
                objDataGrid.Columns.Add("product_ean");
                objDataGrid.Columns.Add("mau_id");
                objDataGrid.Columns.Add("size_id");
                objDataGrid.Columns.Add("so_luong");
                objDataGrid.Columns.Add("gia_goc");
                objDataGrid.Columns.Add("total");
                objDataGrid.Columns.Add("gia_ban_le");
                objDataGrid.Columns.Add("gia_cap1");
                objDataGrid.Columns.Add("gia_cap2");
                objDataGrid.Columns.Add("gia_cap3");
                objDataGrid.Columns.Add("khuyen_mai");

                objDataStaff = clsDbAccess.fncGetAttributeValue(Program.gobjDb, clsEnum.ATTRIBUTE.STAFF);

                grdControlImport.DataSource = objDataGrid;
                txtPersonName.Properties.DataSource = objDataStaff;
                txtDateImport.EditValue = DateTime.Now;

                lukKichThuoc.DataSource = mobjSize;
                lukMauSac.DataSource = mobjMauSac;
                txtKhoHang.Properties.DataSource = mobjKhoHang;

                //hide price
                if (!Program.gblnAdminLoggedIn)
                {
                    colPrice.Visible = false;
                    colTotal.Visible = false;
                }

                dgvImport.BestFitColumns();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnChonSp_Click(object sender, EventArgs e)
        {
            try
            {
                frmChonSp frmChon = new frmChonSp();
                frmChon.evnProductSelected += frmChon_evnProductSelected;
                frmChon.ShowDialog();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        void frmChon_evnProductSelected(List<int> lstProductID, List<string> lstProductName, List<string> lstProductEAN, List<double[]> lstGiaBan, List<int> lstNhaSx)
        {
            try
            {
                DataTable objData = (DataTable)grdControlImport.DataSource;

                for (int i = 0; i <= lstProductID.Count - 1; i++)
                {
                    //neu du lieu da ton tai thi chuyen tiep
                    if (clsCommon.fncDataExist(objData, "product_id", lstProductID[i].ToString())) continue;

                    DataRow objRow = objData.NewRow();
                    objRow["product_id"] = lstProductID[i];
                    objRow["product_name"] = lstProductName[i];
                    objRow["product_ean"] = lstProductEAN[i];
                    objRow["gia_goc"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_GOC];
                    objRow["gia_ban_le"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_LE];
                    objRow["gia_cap1"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_CAP1];
                    objRow["gia_cap2"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_CAP2];
                    objRow["gia_cap3"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_CAP3];
                    objRow["khuyen_mai"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.KHUYEN_MAI];

                    objData.Rows.Add(objRow);
                }

                dgvImport.BestFitColumns();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void mniCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (!clsCommon.fncMsgConfirm("Dữ liệu sẽ không được lưu. Bạn có chắc muốn thoát?")) return;

                this.Close();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void mniSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bool blnSuccess = true;

            try
            {
                if (!objValidate.Validate()) return;

                if (dgvImport.RowCount <= 0)
                {
                    clsCommon.fncMsgWarning("Chưa có sản phẩn nào được chọn.");
                    return;
                }

                if (!xCheckValid()) return;

                if (!clsCommon.fncMsgConfirm("Dữ liệu sẽ được lưu. Bạn có chắc chắn?")) return;

                SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);

                clsDbAccess.stNhapHang stNhap;
                clsDbAccess.stNhapHangChiTiet stNhapChiTiet;
                clsDbAccess.stGiaBan stGia;
                //int intGiaLe;
                //int intGiaCap1;
                //int intGiaCap2;
                //int intGiaCap3;

                stNhap = new clsDbAccess.stNhapHang();
                stNhapChiTiet = new clsDbAccess.stNhapHangChiTiet();
                stGia = new clsDbAccess.stGiaBan();
                
                //luu don hang
                stNhap.intNhaphang_id = clsDbAccess.fncGetMaxNhapHang(Program.gobjDb) + 1;
                stNhap.dtNhaphang_date = clsCommon.fncCnvNullToDatetime(txtDateImport.EditValue);
                stNhap.intNhaphang_nguoinhap = clsCommon.fncCnvNullToInt(txtPersonName.EditValue);
                stNhap.intNhaphang_soluong = clsCommon.fncCnvNullToInt(txtQuantity.Text.Trim());
                stNhap.strNhaphang_ghichu = txtNote.Text.Trim();

                Program.gobjTrans = Program.gobjConn.BeginTransaction();
                try
                {
                    blnSuccess = blnSuccess && clsDbAccess.fncInsertNhapHang(Program.gobjDb, Program.gobjTrans, stNhap);

                    for (int i = 0; i <= dgvImport.RowCount - 1; i++)
                    {
                        stNhapChiTiet.intKho_id = clsCommon.fncCnvNullToInt(txtKhoHang.EditValue);
                        stNhapChiTiet.intNhaphang_id = stNhap.intNhaphang_id;
                        stNhapChiTiet.intProduct_id = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colProductID));
                        stNhapChiTiet.intMau_id = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colMau));
                        stNhapChiTiet.intSize_id = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colSize));
                        stNhapChiTiet.intSo_luong = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colQuantity));
                        stNhapChiTiet.intDon_gia = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colPrice));

                        stGia.intProductID = stNhapChiTiet.intProduct_id;
                        stGia.intGiaGoc = stNhapChiTiet.intDon_gia;
                        stGia.intGiaLe = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colGia_ban_le));
                        stGia.intGiaCap1 = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colGia_cap1));
                        stGia.intGiaCap2 = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colGia_cap2));
                        stGia.intGiaCap3 = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colGia_cap3));
                        stGia.intKhuyenMai = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colKhuyenMai));

                        blnSuccess = blnSuccess && clsDbAccess.fncInsertNhapHangChiTiet(Program.gobjDb, Program.gobjTrans, stNhapChiTiet);

                        //▼▼▼▼▼▼▼ 2014 06 22 ▼▼▼▼▼▼▼▼
                        ////blnSuccess = blnSuccess && clsDbAccess.fncUpdateGiaBan(Program.gobjDb, Program.gobjTrans, stNhapChiTiet.intProduct_id, stNhapChiTiet.intSo_luong, stNhapChiTiet.intDon_gia, intGiaLe, intGiaCap1, intGiaCap2);
                        blnSuccess = blnSuccess && clsDbAccess.fncUpdateIncreaseProductQuantity(Program.gobjDb, Program.gobjTrans, stNhapChiTiet.intProduct_id, stNhapChiTiet.intSo_luong);

                        //blnSuccess = blnSuccess && clsDbAccess.fncUpdateGiaBan2(Program.gobjDb, Program.gobjTrans, stNhapChiTiet.intProduct_id, stNhapChiTiet.intDon_gia, intGiaLe, intGiaCap1, intGiaCap2, intGiaCap3);
                        blnSuccess = blnSuccess && clsDbAccess.fncUpdateGiaBan3(Program.gobjDb, Program.gobjTrans, stGia);
                        blnSuccess = blnSuccess && clsDbAccess.fncUpdateIncreaseProductQuantity2(Program.gobjDb, Program.gobjTrans, stNhapChiTiet);
                        //▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
                    }

                    if (blnSuccess) Program.gobjTrans.Commit();
                    else Program.gobjTrans.Rollback();
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess)
                {
                    clsCommon.fncMsgInfo("Thêm đơn hàng thành công.");
                    this.Close();
                    Program.gevnApplication.RaiseNhapHangUpdate();
                }
                else
                {
                    clsCommon.fncMsgError("Thêm đơn hàng thất bại. Xin hãy thử lại.");
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { SplashScreenManager.CloseForm(false); }
        }

        private void dgvImport_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                int intTotal = 0;

                if (e.Column == colQuantity || e.Column == colPrice)
                {
                    int intQty = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(e.RowHandle, colQuantity));
                    int intSum = intQty * clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(e.RowHandle, colPrice));

                    for (int i = 0; i <= dgvImport.RowCount - 1; i++)
                    {
                        int intQty2 = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colQuantity));
                        intTotal += intQty2;
                    }

                    dgvImport.SetRowCellValue(e.RowHandle, colTotal, intSum);
                    txtQuantity.Text = intTotal.ToString();
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void dgvImport_KeyDown(object sender, KeyEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                if (e.Control && e.KeyCode == Keys.C)
                {
                    //copy row
                    mobjRow = dgvImport.GetFocusedDataRow();
                }

                if (e.Control && e.KeyCode == Keys.V)
                {
                    if (mobjRow == null) return;

                    //paste row
                    DataTable objTempTable = (DataTable)grdControlImport.DataSource;
                    objTempTable.Rows.Add(clsCommon.fncCopyDataRow(objTempTable, mobjRow));
                    dgvImport.FocusedRowHandle = dgvImport.RowCount - 1;
                }

                if (e.KeyCode == Keys.Delete)
                {
                    int intIndex = dgvImport.FocusedRowHandle;
                    if (intIndex < 0) return;

                    //xóa row
                    if (!clsCommon.fncMsgConfirm("Dòng đang được chọn sẽ bị xóa. Bạn có chắc chắn?")) return;
                    dgvImport.DeleteSelectedRows();
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void mniCopy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                //copy row
                mobjRow = dgvImport.GetFocusedDataRow();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void mniPaste_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                if (mobjRow == null) return;

                //paste row
                DataTable objTempTable = (DataTable)grdControlImport.DataSource;
                objTempTable.Rows.Add(clsCommon.fncCopyDataRow(objTempTable, mobjRow));
                dgvImport.FocusedRowHandle = dgvImport.RowCount - 1;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void mniDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                int intIndex = dgvImport.FocusedRowHandle;
                if (intIndex < 0) return;

                //xóa row
                if (!clsCommon.fncMsgConfirm("Bản ghi đang được chọn sẽ bị xóa. Bạn có chắc chắn?")) return;
                dgvImport.DeleteSelectedRows();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }


        private bool xCheckValid()
        {
            bool blnResult = true;
            try
            {
                dgvImport.CloseEditor();
                dgvImport.UpdateCurrentRow();

                DataTable objData = (DataTable)grdControlImport.DataSource;
                string strSelect = "";
                DataRow[] objTemp = null;
                int intProID;
                int intMauID;
                int intSizeID;
                int intSoLuong;

                //kiem tra xem da nhap du mau sac, so luong chua
                for (int i = 0; i <= dgvImport.RowCount - 1; i++)
                {
                    intMauID = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colMau));
                    intSizeID = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colSize));

                    if (intMauID <= 0 || intSizeID <= 0)
                    {
                        clsCommon.fncMsgError("Bạn chưa nhập màu sắc hoặc kích thước của sản phẩm.");
                        dgvImport.FocusedRowHandle = i;
                        return false;
                    }
                }

                //kiem tra xem co dong nao trung lap khong
                for (int i = 0; i <= dgvImport.RowCount - 1; i++)
                {
                    intProID = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colProductID));
                    intMauID = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colMau));
                    intSizeID = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colSize));

                    strSelect = string.Format("product_id = {0} and mau_id = {1} and size_id = {2}", intProID, intMauID, intSizeID);

                    objTemp = objData.Select(strSelect);

                    if (objTemp == null) continue;
                    if (objTemp.Length <= 1) continue;

                    clsCommon.fncMsgError("Thông tin sản phẩm bị trùng lặp.");
                    dgvImport.FocusedRowHandle = i;
                    return false;
                }

                //kiem tra xem so luong la so am?
                for (int i = 0; i <= dgvImport.RowCount - 1; i++)
                {
                    intSoLuong = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colQuantity));

                    if (intSoLuong >= 1) continue;

                    clsCommon.fncMsgError("Số lượng sản phẩm phải lớn hơn 0.");
                    dgvImport.FocusedRowHandle = i;
                    return false;
                }

                return true;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { }
            return blnResult;
        }

        private void btnAddNewProduct_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                frmAddProduct frmNewProduct = new frmAddProduct();
                frmNewProduct.ShowDialog();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void ucSelectProduct1_evnProductSelected(List<int> lstProductID, List<string> lstProductName, List<string> lstProductEAN, List<double[]> lstGiaBan, List<int> lstNhaSx)
        {
            try
            {
                DataTable objData = (DataTable)grdControlImport.DataSource;

                for (int i = 0; i <= lstProductID.Count - 1; i++)
                {
                    //neu du lieu da ton tai thi chuyen tiep
                    if (clsCommon.fncDataExist(objData, "product_id", lstProductID[i].ToString())) continue;

                    DataRow objRow = objData.NewRow();
                    objRow["product_id"] = lstProductID[i];
                    objRow["product_name"] = lstProductName[i];
                    objRow["product_ean"] = lstProductEAN[i];
                    objRow["gia_goc"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_GOC];
                    objRow["gia_ban_le"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_LE];
                    objRow["gia_cap1"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_CAP1];
                    objRow["gia_cap2"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_CAP2];
                    objRow["gia_cap3"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_CAP3];
                    objRow["khuyen_mai"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.KHUYEN_MAI];

                    objData.Rows.Add(objRow);
                    //objData.Rows.InsertAt(objRow, 0);
                }

                dgvImport.BestFitColumns();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            bool blnSuccess = true;

            try
            {
                if (!objValidate.Validate()) return;

                if (dgvImport.RowCount <= 0)
                {
                    clsCommon.fncMsgWarning("Chưa có sản phẩn nào được chọn.");
                    return;
                }

                if (!xCheckValid()) return;

                if (!clsCommon.fncMsgConfirm("Dữ liệu sẽ được lưu. Bạn có chắc chắn?")) return;

                SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);

                clsDbAccess.stNhapHang stNhap;
                clsDbAccess.stNhapHangChiTiet stNhapChiTiet;
                clsDbAccess.stGiaBan stGia;
                //int intGiaLe;
                //int intGiaCap1;
                //int intGiaCap2;
                //int intGiaCap3;

                stNhap = new clsDbAccess.stNhapHang();
                stNhapChiTiet = new clsDbAccess.stNhapHangChiTiet();
                stGia = new clsDbAccess.stGiaBan();

                //luu don hang
                stNhap.intNhaphang_id = clsDbAccess.fncGetMaxNhapHang(Program.gobjDb) + 1;
                stNhap.dtNhaphang_date = clsCommon.fncCnvNullToDatetime(txtDateImport.EditValue);
                stNhap.intNhaphang_nguoinhap = clsCommon.fncCnvNullToInt(txtPersonName.EditValue);
                stNhap.intNhaphang_soluong = clsCommon.fncCnvNullToInt(txtQuantity.Text.Trim());
                stNhap.strNhaphang_ghichu = txtNote.Text.Trim();

                Program.gobjTrans = Program.gobjConn.BeginTransaction();
                try
                {
                    blnSuccess = blnSuccess && clsDbAccess.fncInsertNhapHang(Program.gobjDb, Program.gobjTrans, stNhap);

                    for (int i = 0; i <= dgvImport.RowCount - 1; i++)
                    {
                        stNhapChiTiet.intKho_id = clsCommon.fncCnvNullToInt(txtKhoHang.EditValue);
                        stNhapChiTiet.intNhaphang_id = stNhap.intNhaphang_id;
                        stNhapChiTiet.intProduct_id = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colProductID));
                        stNhapChiTiet.intMau_id = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colMau));
                        stNhapChiTiet.intSize_id = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colSize));
                        stNhapChiTiet.intSo_luong = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colQuantity));
                        stNhapChiTiet.intDon_gia = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colPrice));

                        stGia.intProductID = stNhapChiTiet.intProduct_id;
                        stGia.intGiaGoc = stNhapChiTiet.intDon_gia;
                        stGia.intGiaLe = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colGia_ban_le));
                        stGia.intGiaCap1 = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colGia_cap1));
                        stGia.intGiaCap2 = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colGia_cap2));
                        stGia.intGiaCap3 = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colGia_cap3));
                        stGia.intKhuyenMai = clsCommon.fncCnvNullToInt(dgvImport.GetRowCellValue(i, colKhuyenMai));

                        blnSuccess = blnSuccess && clsDbAccess.fncInsertNhapHangChiTiet(Program.gobjDb, Program.gobjTrans, stNhapChiTiet);

                        //▼▼▼▼▼▼▼ 2014 06 22 ▼▼▼▼▼▼▼▼
                        ////blnSuccess = blnSuccess && clsDbAccess.fncUpdateGiaBan(Program.gobjDb, Program.gobjTrans, stNhapChiTiet.intProduct_id, stNhapChiTiet.intSo_luong, stNhapChiTiet.intDon_gia, intGiaLe, intGiaCap1, intGiaCap2);
                        blnSuccess = blnSuccess && clsDbAccess.fncUpdateIncreaseProductQuantity(Program.gobjDb, Program.gobjTrans, stNhapChiTiet.intProduct_id, stNhapChiTiet.intSo_luong);

                        //blnSuccess = blnSuccess && clsDbAccess.fncUpdateGiaBan2(Program.gobjDb, Program.gobjTrans, stNhapChiTiet.intProduct_id, stNhapChiTiet.intDon_gia, intGiaLe, intGiaCap1, intGiaCap2, intGiaCap3);
                        blnSuccess = blnSuccess && clsDbAccess.fncUpdateGiaBan3(Program.gobjDb, Program.gobjTrans, stGia);
                        blnSuccess = blnSuccess && clsDbAccess.fncUpdateIncreaseProductQuantity2(Program.gobjDb, Program.gobjTrans, stNhapChiTiet);
                        //▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
                    }

                    if (blnSuccess) Program.gobjTrans.Commit();
                    else Program.gobjTrans.Rollback();
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess)
                {
                    clsCommon.fncMsgInfo("Thêm đơn hàng thành công.");
                    this.Close();
                    Program.gevnApplication.RaiseNhapHangUpdate();
                }
                else
                {
                    clsCommon.fncMsgError("Thêm đơn hàng thất bại. Xin hãy thử lại.");
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { SplashScreenManager.CloseForm(false); }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                if (!clsCommon.fncMsgConfirm("Dữ liệu sẽ không được lưu. Bạn có chắc muốn thoát?")) return;

                this.Close();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void frmImportNew_KeyDown(object sender, KeyEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                if (e.Control && e.KeyCode == Keys.N)
                {
                    //them san pham moi
                    Cursor.Current = Cursors.Default;
                    btnAddNewProduct_Click(null, null);
                }

                if (e.Control && e.KeyCode == Keys.S)
                {
                    //luu
                    Cursor.Current = Cursors.Default;
                    btnSave_Click(null, null);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

    }
}